package com.zhuoyue.biz.core.api.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuoyue.biz.core.api.dao.*;
import com.zhuoyue.biz.core.api.domain.*;

import com.zhuoyue.biz.core.api.domain.room.GetRoomAndDeviceCountDO;
import com.zhuoyue.biz.core.api.dto.rep.RoomRepDTO;
import com.zhuoyue.common.exception.ErrorType;
import com.zhuoyue.common.exception.ZyServerException;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author wjming
 * @ClassName: RoomService
 * @Description: 房间服务
 * @Date 2021-07-02 10:04
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RoomService extends ServiceImpl<RoomDao, RoomDO> {
    private final RoomDao roomDao;

    /**
     * 添加房间信息
     *
     * @param room
     * @return
     */
    public RoomRepDTO saveAndReturnRepDto(RoomDO room) {
        boolean save = save(room);
        if (!save) {
            throw new ZyServerException(ErrorType.SAVE_ERROR);
        }
        return new RoomRepDTO().roomDoToRoomDto(room);
    }

    /**
     * 通过家庭ID获取房间信息
     *
     * @param homeId
     * @return
     */
    public List<RoomRepDTO> getRoomsByHomeId(String homeId) {
        List<RoomRepDTO> listRoomDto = new ArrayList<>();
        List<GetRoomAndDeviceCountDO> getRoomAndDeviceCountDos = roomDao.selectRoomAndDeviceCountByRoomId(homeId);

        if (CollectionUtils.isEmpty(getRoomAndDeviceCountDos)) {
            return Collections.emptyList();
        }

        for (GetRoomAndDeviceCountDO roomDo : getRoomAndDeviceCountDos) {
            RoomRepDTO roomRepDto = new RoomRepDTO().getRoomAndDeviceCountDoToRoomDto(roomDo);
            listRoomDto.add(roomRepDto);
        }

        return listRoomDto;
    }

    /**
     * 修改房间信息
     *
     * @param room
     */
    public void updateRoomById(RoomDO room) {
        int count = roomDao.updateRoomSelective(room);
        if (count < 1) {
            throw new ZyServerException(ErrorType.UPDATE_ERROR);
        }
    }

    /**
     * 删除房间信息
     *
     * @param roomId
     */
    public void deleteRoomById(String roomId) {

        int i = roomDao.selectDeviceCountByRoomId(roomId);
        if (i > 0) {
            throw new ZyServerException(ErrorType.Service_Fail.getCode(), "房间中存在设备请先删除设备!");
        }

        int i1 = roomDao.deleteById(roomId);
        if (i1 < 1) {
            throw new ZyServerException(ErrorType.REMOVE_ERROR);
        }
    }

    /**
     * 添加默认房间
     *
     * @param homeId
     */
    public void addDefaultThreeRooms(String homeId) {
        // 新增默认的房间
        List<RoomDO> addRoom = roomObj(homeId, "卧室,客厅,餐厅", "room_01,room_02,room_03");
        boolean b = saveBatch(addRoom);
        if (!b) {
            throw new ZyServerException(ErrorType.OBJECT_NOT_FOUND);
        }
    }


    private List<RoomDO> roomObj(String homeId, String name, String img) {
        String[] split = name.split(",");
        String[] split1 = img.split(",");
        List<RoomDO> roomDos = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            RoomDO roomDO = new RoomDO();
            roomDO.setHomeId(homeId);
            roomDO.setName(split[i]);
            roomDO.setImgUrl(split1[i]);
            roomDos.add(roomDO);
        }
        return roomDos;
    }


}
